package controllers

import (
	"github.com/kataras/iris/v12"
	log "github.com/sirupsen/logrus"
	"github.com/spf13/viper"
	"irisfarm/app/services"
	"irisfarm/app/validates"
)

type ExampleController struct {
	BaseController
}

//NewExampleController 初始化ExampleController
func NewExampleController(ctx iris.Context) *ExampleController {
	c := new(ExampleController)
	c.InitContext(ctx)
	return c
}

// Index example index
func (c *ExampleController) Index() {
	c.output.Success("this is a example page!")
}

// Model example model
func (c *ExampleController) Model() {
	es := services.ExampleServer{}

	c.output.Success(es.GetName())
}

// Config example config
func (c *ExampleController) Config() {
	c.output.Success(viper.GetString("mysql.driver"))
}

// Log example log
func (c *ExampleController) Log() {
	log.Info("you will see me here")

	c.output.Success("write a log into log file")
}

// Repository example repository
func (c *ExampleController) Repository() {
	es := services.ExampleServer{}
	result, err := es.SayHello()

	if err != nil {
		c.output.Error(err.Error())
		return
	}

	c.output.Success(result)
}

// Validate example validate
func (c *ExampleController) Validate() {
	// 数据校验
	vErr := validates.ExampleRequestValidate(c.ctx)
	if vErr != nil {
		c.output.Error(vErr.Error())
		return
	}

	c.output.Success("validate success")
}
